package com.course.domain.dao;

import com.course.domain.model.ClassPlan;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * Created by Justin on 2017/5/25.
 */
public interface ClassPlanDao extends JpaRepository<ClassPlan, Integer> {

    List<ClassPlan> findByStudentIdAndCourseIdOrderByDate(int studentId, int courseId);

    List<ClassPlan> findByStudentIdAndCourseIdAndStateOrderByDate(int studentId, int courseId, int state);

    List<ClassPlan> findByTeacherIdOrderByDate(int teacherId);

    ClassPlan findFirstByStudentIdAndCourseIdAndDateAfterOrderByDate(int studentId, int courseId, Date date);

    ClassPlan findFirstByTeacherIdAndDateAfterOrderByDate(int teacherId, Date date);

    Page<ClassPlan> findByStudentIdAndCourseIdAndState(int studentId, int courseId, int state, Pageable pageable);

    @Modifying
    @Query("update ClassPlan plan set plan.teacherId = ?2 where plan.id = ?1")
    int updateTeacher(int planId, int teacherId);

    @Modifying
    @Query("update ClassPlan plan set plan.room = ?2 where plan.id = ?1")
    int updateRoom(int planId, String room);

    @Modifying
    @Query("update ClassPlan plan set plan.mark = ?2, plan.state=1 where plan.id = ?1")
    int updateMark(int planId, String comment);
}
